Repository navigation
Conversation
Add mz_internal.mz_inferred_relationships, a builtin materialized view on mz_catalog_server that reads the relationships between user relations out of the environment's view and materialized view definitions, with no declared keys. A new scalar function, mz_internal.parse_catalog_view_joins, walks a persisted create_sql's resolved AST and returns the column pairs the view equates (ON, WHERE, USING, and IN (SELECT ...) semijoins) and its output columns that are input columns unchanged. Qualifiers resolve through a stack of SELECT scopes, and anything not bound to a catalog relation (a CTE, a subquery, a table function, an alias that renames columns) is opaque, so the function errs toward missing a relationship. The view orients each join toward a column that alone keys an index, then a column named id, then away from a column named *_id, and keeps a join nothing settles undirected. A passed-through key references its source, and a passed-through non-key column shares its references with its source, to a fixed point with WITH MUTUALLY RECURSIVE. Propagating through keys too would build the transitive closure of a view stack, which grows with the square of its depth. The function is gated by a new enable_inferred_relationships feature flag, default off, on in CI. The builtin is planned with item parsing flags on, so it is maintained regardless of the flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jubrad
added a commit
to jubrad/materialize
that referenced
this pull request
Nov 16, 2026
A passed key now gets only its direct reference. Propagating through it too built the transitive closure of a view stack, which grows with the square of its depth: a stack of 300 views passing `id` produced 45,150 relationships. This keeps the explorer in agreement with mz_internal.mz_inferred_relationships (MaterializeInc#40832). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jubrad
added a commit
to jubrad/materialize
that referenced
this pull request
Nov 17, 2026
Inference no longer derives references to a fixed point. Every inferred edge now comes from a single view's definition: a join, a passed key, or a new "same values" edge for any other passed-through column. This matches mz_internal.mz_inferred_relationships (MaterializeInc#40832), where the fixpoint cost about twice the maintenance per replaced view and the derived edges added nothing the paths lack. Declared joins are confirmed by a path of inferred edges, and only same-values edges on a path to a join are drawn, so passed-through names and amounts do not bury the relationships. On the auction demo every declared join that resolves is connected, against 9 of 11 with the fixpoint. Above 500 entities the canvas is no longer drawn: dagre's recursive ranking overflowed the stack on a chain of about 3,000 views. A large environment now opens on the ontology table's schemas, or the one schema with the most inferred joins, rather than every schema an inferred join touches. Also restores the license header of inferredJoins.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the WITH MUTUALLY RECURSIVE fixpoint. Every row now comes from a single view's definition, with a new `kind` column: `join` for columns a view equates, `key` for a key passed through, and `same_values` for any other column passed through. Relationships across several views are the paths through these rows. Maintaining the fixpoint was nearly all of the view's cost. Replacing 300 views with 3,000 present cost 1.66 ms of catalog server CPU per view with it and 0.68 ms without, against 0.57 ms for mz_object_dependencies, and hydration no longer outlasts mz_views. Paths lose nothing: on the auction demo, every relationship the fixpoint derived is connected, and so is every resolvable row of a hand-written ontology table. Also drops the column existence joins, since the planner validated every column a persisted definition names, and the pass that dropped undirected duplicates, since direction depends only on the two columns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add mz_internal.mz_mcp_relationships, a builtin view that lists the relationships between relations the current role can read, shaped like an ontology table (table_name, columns, referenced_table, referenced_columns), so agents can find how to join two relations without one. It is on the restrict_to_user_objects allow-list, and like mz_mcp_data_products lists only relations the role holds SELECT on. Besides the join and key rows of mz_inferred_relationships, it lists derived references: a column holding the same values as a referencing column, through a chain of views passing it, references the same key, so fct_orders.customer_id references customers.id even when only an intermediate view joins them. The chains are walked on read, by pointer doubling over the same_values rows. Maintaining them in the materialized view instead measured 3 to 5.5 times its cost per replaced view; on read the view takes about 300 ms with 4,000 views. Also drop the enable_inferred_relationships flag. Builtins are created regardless of flags, so it gated only the parse function, not the cost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A passed-through column is not a relationship. It is the link mz_mcp_relationships follows to derive references, so name it for what it records and document that agents should read mz_mcp_relationships. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two builtins that read the relationships between user relations out of their view and materialized view definitions, with no declared keys or ontology table:
mz_internal.mz_inferred_relationships, a materialized view onmz_catalog_serverholding direct edges.mz_internal.mz_mcp_relationships, a view for agents, shaped like an ontology table, filtered to what the session's role can read, and on therestrict_to_user_objectsallow-list.It's the backend counterpart to the Console ontology explorer (#40712), which applies the same rules in the browser.
How it works
mz_internal.parse_catalog_view_joins(create_sql)follows the existingparse_catalog_*pattern inmz-catalog-decode. It walks a persistedcreate_sql's resolved AST and returns JSON with three lists:ON,WHERE,USING, andIN (SELECT ...)semijoins);USINGjoins, with the relations joined before them;Qualifiers resolve through a stack of
SELECTscopes. Anything that isn't bound to a catalog relation is opaque, so the function errs toward missing a relationship rather than inventing one:mz_internal.mz_inferred_relationshipsholds only direct edges. Every row comes from a single view's definition, so the view grows linearly with the number of views. Thekindcolumn says which of three it is:kindjoinid, then the side not named*_id. A join none of these settle hasis_directed = false.bids.auction_id → auctions.idkeySELECT o.id AS org_idgivesorg_spend.org_id → organizations.idpassthroughmz_mcp_relationshipsfollows to derive references.winning_bids.buyer → bids.buyerRelationships across several views are paths through these rows. Nothing is derived in the view: an earlier revision derived them with
WITH MUTUALLY RECURSIVE, and maintaining that fixpoint was most of its cost (numbers below).mz_internal.mz_mcp_relationshipsis what agents query, with the same columns as the ontology table pattern (table_name,columns,referenced_table,referenced_columns, pluskindandis_directed). Besides thejoinandkeyrows, it listsderivedreferences. A column holding the same values as a referencing column, through a chain of views passing it, references the same key. So in a dbt-style project every layer gets the foreign key directly:stg_orders.customer_id,int_orders.customer_idandfct_orders.customer_ideach→ customers.id, even when only one layer joinscustomers. Agents get parity with a hand-written ontology table without writing recursive queries.passthroughrows. Maintaining them in the materialized view measured 3 to 5.5 times its cost per replaced view (table below). On read, the view takes about 300 ms with 4,000 views.mz_mcp_data_products, rows are limited to relations the role holdsSELECTon, throughmz_show_my_object_privileges, so the view can be allow-listed forrestrict_to_user_objectssessions without describing relations the role can't see. A derived row between two readable relations can depend on a chain through views the role can't read. Those views' names never appear, only the relationship they imply.No feature flag: builtins are created whatever the flags say, so a flag could only gate the parse function, not the cost.
Validation against the Console
On the auction demo, with six extra views covering a CTE, alias shadowing,
USING, a semijoin and expression joins, I pointed the Console from #40712 at a localenvironmentdrunning this branch and compared by kind.joinandkeyrows match the Console's inferred joins, with two expected differences:USING (org_id)pair. Its order is arbitrary, and the two sides pick differently.SELECT a.id FROM auctions a JOIN (SELECT a.id FROM bids a) s ON .... The Console's token scan seesabound twice and gives up. The AST walk scopes the innerato its subquery.passthroughrows: this view stores 14. The Console never draws them, only the references derived through them.Coverage without derivation. Two columns count as related when a path of rows connects them.
Cost (local, optimized build,
mz_catalog_serverwith 1 worker, about 3,000 views)CPU is each dataflow's root operator in
mz_scheduling_elapsed. Summing every operator double-counts nested scopes, which inflated the figures in this PR's first revision.mz_object_dependenciesCREATE OR REPLACEin one burstmz_viewsWhere derived references are computed, with about 4,200 views (the chain plus a dbt-style workload and a 300-deep stack):
WITH MUTUALLY RECURSIVEmz_mcp_relationships(shipped)CREATE VIEWreturning, as fast asmz_object_dependencies.Tests
view_joins.rsunit tests: extraction from realcreate_sql, including what must be skipped.mz_inferred_relationships.slt:DROP VIEW;mz_mcp_relationships;restrict_to_user_objectsrole that sees only the relations it can read, and is refusedmz_inferred_relationships.regproc,system-parameters.json, the autogenerated catalog SLT, and the catalog_server EXPLAIN, index accounting, OID andinformation_schemaSLTs (builtin row counts only).Open questions
mz_object_dependencies.mz_mcp_relationships, for example in its instructions or a tool, the way the ontology table docs give agents a system prompt?Release notes
None: both views are in
mz_internal, which is unstable.🤖 Generated with Claude Code